package relation

import (
	"dy/pkg/code"
	"dy/pkg/jwt"
	"github.com/gin-gonic/gin"
)

func (u *ControllerRelation) FriendList(ctx *gin.Context) {
	type param struct {
		UserId int    `form:"user_id" json:"user_id" binding:"required"`
		Token  string `form:"token" json:"token" binding:"required"`
	}

	var request param
	err := ctx.Bind(&request)
	if err != nil {
		code.ErrReturn(ctx, err.Error())
		return
	}
	userId, err := jwt.ParseToken(request.Token)
	if err != nil {
		code.ErrReturn(ctx, err.Error())
		return
	}
	if userId != request.UserId {
		code.ErrReturn(ctx, "user_id or token err")
		return
	}

	list, err := u.srv.Relation().FriendList(ctx, request.UserId)
	if err != nil {
		code.ErrReturn(ctx, err.Error())
		return
	}

	ctx.JSON(200, gin.H{
		"status_code": 0,
		"status_msg":  "success",
		"user_list":   list,
	})
	return
}
